package cn.xinfei.xdecision.manager.controller.rejectcode;

import cn.xinfei.xdecision.common.model.common.ResponseEntityBuilder;
import cn.xinfei.xdecision.common.model.common.ResponseEntityDto;
import cn.xinfei.xdecision.common.model.configcenter.rejectcode.vo.*;
import cn.xinfei.xdecision.common.model.login.SessionManager;
import cn.xinfei.xdecision.common.service.common.sso.SSOService;
import cn.xinfei.xdecision.common.service.common.sso.dto.SSOUserInfoRespDto;
import cn.xinfei.xdecision.common.service.configcenter.RejectBranchCodeService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RequestMapping("/v3/reject-code")
@RestController("rejectBranchCodeControllerV3")
public class RejectBranchCodeController {

    @Autowired
    RejectBranchCodeService rejectBranchCodeService;

    @Autowired
    SSOService ssoService;

    @PostMapping("category")
    @ApiOperation(value = "新增所属大类", notes = "新增所属大类")
    public ResponseEntityDto<Void> createCategory(@RequestBody RejectCategoryCreateAndUpdateReq rejectCategoryCreateAndUpdateReq) {
        log.info("createCategory param:{}", rejectCategoryCreateAndUpdateReq);
        return ResponseEntityBuilder.buildNormalResponse(rejectBranchCodeService.createCategory(rejectCategoryCreateAndUpdateReq));
    }

    @GetMapping("category")
    @ApiOperation(value = "查询类别列表", notes = "查询类别列表")
    public ResponseEntityDto<RejectCategoryListResp> getCategoryList(@Param("parentId") Long parentId) {
        log.info("getCategoryList param:{}", parentId);
        RejectCategoryListResp categoryList = rejectBranchCodeService.getCategoryList(parentId);
        return ResponseEntityBuilder.buildNormalResponse(categoryList);
    }

    @PostMapping("code")
    @ApiOperation(value = "新增拒绝码", notes = "新增拒绝码")
    public ResponseEntityDto createCode(@RequestBody RejectCodeCreateAndUpdateReq rejectCategoryCreateAndUpdateReq) {
        log.info("createCode param:{}", rejectCategoryCreateAndUpdateReq);
        rejectBranchCodeService.createCode(rejectCategoryCreateAndUpdateReq);
        return ResponseEntityBuilder.buildNormalResponse();
    }

    @GetMapping("code")
    @ApiOperation(value = "查询拒绝码列表", notes = "查询拒绝码列表")
    public ResponseEntityDto<RejectCodeListResp> getBranchCodeList(@Param("categoryId") Long categoryId) {
        log.info("getBranchCodeList param:{}", categoryId);
        RejectCodeListResp branchCodeList = rejectBranchCodeService.getBranchCodeList(categoryId);
        return ResponseEntityBuilder.buildNormalResponse(branchCodeList);
    }

    @PostMapping("branch")
    @ApiOperation(value = "新增拒绝分支码", notes = "新增拒绝分支码")
    public ResponseEntityDto<Void> createBranchCode(@RequestBody RejectBranchCodeCreateAndUpdateReq rejectBranchCodeCreateAndUpdateReq) {
        Long userId = SessionManager.getLoginUserId();
        log.info("createBranchCode param:{},{}", userId, rejectBranchCodeCreateAndUpdateReq);
        return ResponseEntityBuilder.buildNormalResponse(rejectBranchCodeService.createBranchCode(userId, rejectBranchCodeCreateAndUpdateReq));
    }

    @GetMapping("branch/{id}")
    @ApiOperation(value = "根据id查询拒绝分支码明细", notes = "根据id查询拒绝分支码明细")
    public ResponseEntityDto<RejectBranchCodeResp> getBranchCode(@PathVariable("id") Long id) {
        log.info("getBranchCode param:{}", id);
        RejectBranchCodeResp branchCode = rejectBranchCodeService.getBranchCode(id);

        SSOUserInfoRespDto ssoUserInfoRespDto = ssoService.getUserInfoById(branchCode.getCreateUserId());
        if (ssoUserInfoRespDto != null) {
            branchCode.setCreatedByName(ssoUserInfoRespDto.getName());
        }
        return ResponseEntityBuilder.buildNormalResponse(branchCode);
    }

    @PostMapping("branch/{id}")
    @ApiOperation(value = "根据id更新拒绝分支码明细", notes = "根据id更新拒绝分支码明细")
    public ResponseEntityDto<Void> updateBranchCode(@PathVariable("id") Long id, @RequestBody RejectBranchCodeCreateAndUpdateReq rejectBranchCodeCreateAndUpdateReq) {
        Long userId = SessionManager.getLoginUserId();
        log.info("updateBranchCode param:{},{},{}", userId, id, rejectBranchCodeCreateAndUpdateReq);
        return ResponseEntityBuilder.buildNormalResponse(rejectBranchCodeService.updateBranchCode(userId, id, rejectBranchCodeCreateAndUpdateReq));
    }

    @DeleteMapping("branch/{id}")
    @ApiOperation(value = "根据id删除拒绝分支码", notes = "根据id删除拒绝分支码")
    public ResponseEntityDto removeBranchCode(@PathVariable("id") Long id) {
        log.info("removeBranchCode param:{},", id);

        rejectBranchCodeService.removeBranchCode(id);
        return ResponseEntityBuilder.buildNormalResponse();
    }

    @GetMapping("branch/search")
    @ApiOperation(value = "拒绝分支码查询", notes = "拒绝分支码查询")
    public ResponseEntityDto<Map<String, Object>> searchBranchCode(RejectBranchCodeSearchReq rejectBranchCodeSearchReq) {

        log.info("searchBranchCode param:{},", JSON.toJSONString(rejectBranchCodeSearchReq));

        PageInfo<RejectBranchCodeResp> pageInfo = rejectBranchCodeService.searchBranchCode(rejectBranchCodeSearchReq);

        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("pager", pageInfo);
        hashMap.put("branchCodeList", pageInfo.getList());
        return ResponseEntityBuilder.buildNormalResponse(hashMap);

    }

    @PostMapping("diversion-label")
    @ApiOperation(value = "新增导流标签", notes = "新增导流标签")
    public ResponseEntityDto createDiversionLabel(@RequestBody RejectDiversionLabelCreateAndUpdateReq rejectDiversionLabelCreateAndUpdateReq) {
        log.info("createDiversionLabel param:{},", rejectDiversionLabelCreateAndUpdateReq);
        rejectBranchCodeService.createDiversionLabel(rejectDiversionLabelCreateAndUpdateReq);
        return ResponseEntityBuilder.buildNormalResponse();
    }

    @PostMapping("diversion-label/{id}")
    @ApiOperation(value = "根据id更新导流标签", notes = "根据id更新导流标签")
    public ResponseEntityDto updateDiversionLabel(@PathVariable("id") Long id, @RequestBody RejectDiversionLabelCreateAndUpdateReq rejectDiversionLabelCreateAndUpdateReq) {
        log.info("updateDiversionLabel param:{},{}", id, rejectDiversionLabelCreateAndUpdateReq);
        rejectBranchCodeService.updateDiversionLabel(id, rejectDiversionLabelCreateAndUpdateReq);
        return ResponseEntityBuilder.buildNormalResponse();
    }

    @GetMapping("diversion-label/page")
    @ApiOperation(value = "查询导流标签", notes = "查询导流标签")
    public ResponseEntityDto<Map<String, Object>> getDiversionLabel(@RequestParam(defaultValue = "1") Integer pageNo,
                                                                    @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<RejectDiversionLabelResp> pageInfo = rejectBranchCodeService.getDiversionLabel(pageNo, pageSize);

        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("pager", pageInfo);
        hashMap.put("diversionLabelList", pageInfo.getList());
        return ResponseEntityBuilder.buildNormalResponse(hashMap);
    }

    @GetMapping("diversion-label/all")
    @ApiOperation(value = "查询所有导流标签", notes = "查询所有导流标签")
    public ResponseEntityDto<RejectDiversionLabelListResp> getDiversionLabelAll() {
        RejectDiversionLabelListResp rejectDiversionLabelListResp = rejectBranchCodeService.getRejectDiversionLabelAll();
        return ResponseEntityBuilder.buildNormalResponse(rejectDiversionLabelListResp);
    }


}
